const log = require('../util/log')

module.exports = (req, res) => {
  require('../util/service').handlePost(req, res, (reqObj, resolve) => {
	const mongoMng = require('../mng/mongoMng')
    if (!mongoMng.db) {
      log('未连接mongo')
      resolve({
        result: -1,
        msg: '系统错误'
      })
      return;
    }
    mongoMng.db.collection('bill').aggregate([{
      $group: {
        _id: '$customer_phone',
        sum: {$sum: '$returnMoney'},
        count: {$sum: 1}
      }
    }, {
      $sort: {
        sum: -1
      }
    }]).toArray((err, result) => {
      if (err) {
        log(err)
        resolve({
          result: -1,
          msg: '系统错误'
        })
        return
      }
      result.forEach(item => {
        item.customer_phone = item._id
        delete item._id
        item.sum = item.sum.toFixed(2)
      })
      resolve({
        data: result
      })
    })
  })
}
